package com.example.bookstore.dao.impl;

import com.example.bookstore.dao.BookDao;
import com.example.bookstore.pojo.Book;

import java.sql.SQLException;
import java.util.List;

public class BookDaoImpl extends BaseDao<Book> implements BookDao {

    @Override
    public int addBook(Book book) throws SQLException {
        String sql = "insert into t_book(`name`,`price`,`author`,`sales`,`stock`,`img_path`) values(?,?,?,?,?,?)";
        return update(sql,book.getName(),book.getPrice(),book.getAuthor(),book.getSales(),book.getStock(),book.getImg_path());
    }

    @Override
    public int deleteBookById(Integer id) throws SQLException {
        String sql = "delete from t_book where `id` = ?";
        return update(sql,id);
    }

    @Override
    public int updateBook(Book book) throws SQLException {
        String sql = "update t_book set `name` = ?,`price` = ?,`author` = ?,`sales` = ?,`stock` = ?,`img_path` = ? where `id` = ?";
        return update(sql,book.getName(),book.getPrice(),book.getAuthor(),book.getSales(),book.getStock(),book.getImg_path(),book.getId());
    }

    @Override
    public Book queryBookById(Integer id) throws SQLException {
        String sql = "select * from t_book where `id` = ?";
        return querySingle(sql,Book.class,id);
    }

    @Override
    public List<Book> queryBooks() throws SQLException {
        String sql = "select * from t_book";
        return queryMulti(sql, Book.class);
    }

    @Override
    public Integer queryForPageTotalCount() throws SQLException {
        String sql = "select count(*) from t_book";
        Number num = (Number) queryScalar(sql);
        return num.intValue();
    }

    @Override
    public List<Book> queryForPageItems(int begin, int pageSize) throws SQLException {
        String sql = "select * from t_book limit ? , ?";
        return queryMulti(sql,Book.class,begin,pageSize);
    }
}
